﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bluyel.Core
{
    /// <summary>
    /// represents a data structure.
    /// </summary>
    public class DStruct : DataNamedBase, IDStruct
    {
        /// <summary>
        /// List of dataNamed members of the structure.
        /// </summary>
        //List<IDataNamed> _listDataNamed = new List<IDataNamed>();
        ListData _listData = new ListData();

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="name"></param>
        public DStruct(string name, IType type, VarOrConst varOrConst):base(name, type, varOrConst)
        {}

        /// <summary>
        /// Constructor.
        /// A var by default.
        /// </summary>
        /// <param name="name"></param>
        public DStruct(string name, IType type)
            : base(name, type, VarOrConst.Var)
        { }

        /// <summary>
        /// Return the list of data named members.
        /// </summary>
        public IEnumerable<IDataNamed> ListData
        { get { return _listData.ListData; } }

        /// <summary>
        /// Add a new data member to the structure.
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool AddMember(IDataNamed data)
        {
            if (data == null)
                return false;
            // check if the same member was not already added
            // TODO:

            _listData.Add(data);
            return true;
        }

        /// <summary>
        /// remove the member, by the name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool RemoveMember(string name)
        {
            return _listData.Remove(name); 
        }

        /// <summary>
        /// remove the member, by the instance.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool RemoveMember(IDataNamed d)
        {
            return _listData.Remove(d);
        }

        /// <summary>
        /// Find a data member, by the name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IDataNamed FindMember(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
                return null;

            return _listData.FindDataByName(name);
        }

        /// <summary>
        /// exp: 
        /// st=DatStr:3m
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string s = Name + "=DatStr:" + _listData.Count+"m";
            return s;
        }
    }
}
